package com.cangxuan.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cangxuan.app.R;
import com.cangxuan.app.entity.Collections;
import com.cangxuan.app.ui.collection.CollectionDetailActivity;
import com.cangxuan.app.utils.HttpUtils;

import java.io.File;
import java.util.List;

public class HomeSaleCalendarAdapter extends BaseAdapter {

    private List<Collections> collectionList;
    private Context context;
    private Handler handler = new Handler();


    public HomeSaleCalendarAdapter(List<Collections> collectionList, Context context) {
        this.collectionList = collectionList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return collectionList.size();
    }

    @Override
    public Object getItem(int i) {
        return collectionList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return collectionList.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.fragment_home_list_sale_date_item, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.collectionName = view.findViewById(R.id.collectionName);
            viewHolder.collectionPrice = view.findViewById(R.id.collectionPrice);
            viewHolder.collectionNum = view.findViewById(R.id.collectionNum);
            viewHolder.collectionImg = view.findViewById(R.id.collection_img);
            viewHolder.releaseDate = view.findViewById(R.id.releaseDate);
            viewHolder.releaseTime = view.findViewById(R.id.releaseTime);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Collections collection = collectionList.get(i);
        viewHolder.collectionName.setText(collection.getCollectionName());
        viewHolder.collectionPrice.setText(String.valueOf(collection.getOfferPrice()));
        viewHolder.collectionNum.setText(String.valueOf(collection.getMintage()));
        String month = collection.getReleaseDate().getMonthValue() < 10 ? "0"+collection.getReleaseDate().getMonthValue() : collection.getReleaseDate().getMonthValue()+"";
        String day = collection.getReleaseDate().getDayOfMonth() < 10 ? "0"+collection.getReleaseDate().getDayOfMonth() : collection.getReleaseDate().getDayOfMonth()+"";
        String hour = collection.getReleaseDate().getHour()<10 ? "0" + collection.getReleaseDate().getHour() : collection.getReleaseDate().getHour()+"";
        String minute = collection.getReleaseDate().getMinute() < 10 ? "0"+collection.getReleaseDate().getMinute() : collection.getReleaseDate().getMinute()+"";
        viewHolder.releaseDate.setText(month+"月"+day+"日");
        viewHolder.releaseTime.setText(hour+":"+minute);
        //图片文件路径，格式：/data/data/com.teacher.hong/upload/goods/1.jpg
        String goodsImagePath = "/data/data/" + context.getPackageName() + collection.getImage();
        Log.i("下标为" + i + "的图片文件路径", goodsImagePath);
        File imageFile = new File(goodsImagePath);
        if (imageFile.exists()) {
            //图片已下载过，已存在
            viewHolder.collectionImg.setImageBitmap(BitmapFactory.decodeFile(goodsImagePath));
        } else {
            //图片未下载过，进行下载
            Thread thread = new Thread(() -> {
                boolean result = HttpUtils.download(collection.getImage(), goodsImagePath);
                if (result) {
                    handler.post(() -> {
                        viewHolder.collectionImg.setImageBitmap(BitmapFactory.decodeFile(goodsImagePath));
                    });
                }
            });
            thread.start();
        }
        view.setOnClickListener(view1 -> {
            Intent intent = new Intent();
            intent.setClass(context, CollectionDetailActivity.class);
            intent.putExtra("isHome", true);
            intent.putExtra("goodsId", collectionList.get(i).getId());
            context.startActivity(intent);
        });
        return view;
    }

    class ViewHolder {
        ImageView collectionImg;
        TextView collectionName;
        TextView collectionNum;
        TextView collectionPrice;
        TextView releaseDate;
        TextView releaseTime;
    }

}
